/**
 * 
 */
package com.soap;


import java.io.IOException;
import java.net.SocketTimeoutException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import com.clases.Imagen;


/**
 * @author Chris
 * 
 */
public class Soap_Imagenes {

    private static String TAG = "SOAP IMAGENES";

    SoapSerializationEnvelope envelope;

    PropertyInfo pi;

    SoapObject Request;

    public Imagen[] getImagenes(int lugar) {

        String METHOD_NAME = "getimagenes";
        String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

        Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);
        Request.addProperty("id_lugar", lugar);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL, SoapUtil.TIMEOUT);

        Object response = null;

        try {

            httpTransport.call(SOAP_ACTION, envelope);
            response = envelope.getResponse();

            return getInformacion(response);

        }
        catch (SocketTimeoutException x) {
            Log.e(TAG + ": ", x.getMessage());

            Imagen[] imgs = new Imagen[1];
            Imagen cat = new Imagen();
            cat.setIDImagen(-10);
            imgs[0] = cat;
            return imgs;
        }
        catch (SoapFault e) {
            StackTraceElement[] stack = e.getStackTrace();
            String x = " ";
            for (StackTraceElement line : stack) {
                x += line.toString();
            }
            Log.e(TAG + ": ", x);

            Imagen[] imgs = new Imagen[1];
            Imagen cat = new Imagen();
            cat.setPath(x);
            imgs[0] = cat;
            return imgs;

        }
        catch (IOException x) {
            Log.e(TAG + ": ", x.getMessage());

            Imagen[] imgs = new Imagen[1];
            Imagen cat = new Imagen();
            cat.setPath(x.getMessage());
            imgs[0] = cat;
            return imgs;
        }
        catch (XmlPullParserException x) {
            Log.e(TAG + ": ", x.getMessage());

            Imagen[] imgs = new Imagen[1];
            Imagen cat = new Imagen();
            cat.setPath(x.getMessage());
            imgs[0] = cat;
            return imgs;
        }
    }

    private Imagen[] getInformacion(Object soap) {

        String RES = soap.toString();
        try {
            JSONArray jsonArray = new JSONArray(RES);
            Imagen[] imgs = new Imagen[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo = new JSONObject(jsonArray.getString(i));
                int IDImagen = jo.getInt("ID_Images");
                String Path = jo.getString("path");

                Imagen img = new Imagen();
                img.setIDImagen(IDImagen);
                img.setPath(Path);

                imgs[i] = img;

            }

            return imgs;
        }
        catch (Exception x) {

            Imagen[] imgs = new Imagen[1];
            Imagen cat = new Imagen();
            cat.setPath(x.getMessage());
            imgs[0] = cat;
            return imgs;

        }
    }
}
